-
Notifications
You must be signed in to change notification settings - Fork 97
Update Happy_Number.js #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi thanks for contributing.
I think it's enough to check just for 1
since 7 will eventually hit n === 1
. I like the example, we can merge that 👍.
@@ -40,7 +49,7 @@ function checkHappyNumber(n){ | |||
let strNumber = n.toString(); | |||
let splitNumber = strNumber.split(""); | |||
if(splitNumber.length <= 1){ | |||
return (n <= 1)? true:false; | |||
return (n === 1 || n === 7)? true:false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we'd need to check for 7
since it's already covered by n===1
.
return (n === 1 || n === 7)? true:false; | |
return n === 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
Thanks for getting back to me, the thing is the first if
condition checks whether the array's length is 1 or not,
in this case if the array has only 7
then it would be true, and since it is to return true
if n===1
it would return false.
I have added some screenshots for tests please feel free to check them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ohhhh true! Nice catch. We need to list all happy numbers that are less than 10.
@@ -40,7 +49,7 @@ function checkHappyNumber(n){ | |||
let strNumber = n.toString(); | |||
let splitNumber = strNumber.split(""); | |||
if(splitNumber.length <= 1){ | |||
return (n <= 1)? true:false; | |||
return (n === 1 || n === 7)? true:false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ohhhh true! Nice catch. We need to list all happy numbers that are less than 10.
Thank you @Alucard2169 |
No problem. |
Added base case check for Happy number problem.
About the fix.
7
Is also considered a Happy number, previously the code was checking if the array length is1
andn
was1
then returntrue
elsefalse
.